﻿using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MvcSample.Middlewares
{
    public class LogExceptionMiddleware
    {
        private readonly RequestDelegate _requestDelegate;
        private readonly ILogger<LogExceptionMiddleware> _logger;
        public LogExceptionMiddleware(RequestDelegate requestDelegate,
            ILogger<LogExceptionMiddleware> logger)
        {
            _requestDelegate = requestDelegate;
            _logger = logger;
        }
        public async Task InvokeAsync(HttpContext context)
        {
            try
            {
                await _requestDelegate.Invoke(context);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                _logger.LogError(ex, "Internal error.");
            }
        }
    }
}
